/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A TrackICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "CScore.h"
#include "CScoreView.h"
#include "CEasyScoreApp.h"
#include "CPart.h"
#include "CPartFactory.h"
#include "CBar.h"

namespace easyscore
{
	namespace core
	{
		/*!
			\fn easyscore::core::CScore::CScore()
			\brief CScore constructor
		*/
		CScore::CScore(CEasyScoreApp *parentApp)
		{
			mParentApp = parentApp;
			mModified = false;
			mInitialized = false;
			
			mView = 0;
		}

		/*!
			\fn easyscore::core::CScore::initialize()
			\brief CScore initialization
		 */
		void CScore::initialize ( CScoreSettings *settings )
		{
			if ( settings != 0 )
			{
				mAuthorName = settings->getAuthorName();
				mSongname = settings->getSongName();
				mTempo = settings->getTempo();
				mTime = settings->getTime();

				int order = 0;
				foreach ( CInstrument* instrument,settings->getInstrumentList() )
				{
					CPart *currentPart = CPartFactory::createPart(instrument->getInstrument());

					currentPart->setParentScore ( this );
                                        //currentPart->setOrder ( ++order );
					currentPart->initialize();
					
					mParts.append ( currentPart );
				}
			}

                        mInitialized = true;
		}

		/*!
			\fn easyscore::core::CScore::clear()
		 */
		void CScore::clear()
		{
			mInitialized = false;
			
			foreach ( CPart * part, mParts )
			{
				delete part;
			}
			mParts.clear();
			
			destroyView();
		}

		/*!
			\fn easyscore::core::CScore::CScore()
			\brief CScore destructor
		*/
		CScore::~CScore()
		{
			clear();
		}

                void CScore::addPart(CPart *part)
                {
                    part->setParentScore(this);
                    mParts.append(part);
                }

                QLinkedList<CTrack *> CScore::getTracks()
                {
                    QLinkedList<CTrack *> result;
                    foreach(CPart *part, mParts)
                    {
                        result += part->getTracks();
                    }

                    return result;
                }

                QLinkedList<CBar *> CScore::getBars()
                {
                    QLinkedList<CTrack *> tracks = getTracks();
                    QLinkedList<CBar *> result;
                    foreach(CTrack *track, tracks)
                    {
                        result += track->getBars();
                    }

                    return result;
                }


		/*!
			\fn easyscore::core::CScore::createView()
			\brief creates and initializes a view
		 */
		void CScore::createView()
		{
			destroyView();

			mView = new CScoreView ( this );
			mView->initialize();
		}

		/*!
			\fn easyscore::core::CScore::destroyView()
			\brief destroys a view
		*/
		void CScore::destroyView()
		{
			foreach(CPart *part, mParts)
			{
				part->destroyView();
			}
			
			if ( mView != 0 )
			{
				delete mView;
				mView = 0;
			}
		}
		
	}// namespace core
}// namespace easyscore






